package net.huashitong.Indicators.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.dto.StudentDTO;
import net.huashitong.Indicators.entity.AB01;
import net.huashitong.Indicators.interfacedao.SequenceDao;
import org.hibernate.SQLQuery;
import org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.ServiceLoader;

@Repository
public class AB01Dao extends StringDao<AB01> implements SequenceDao {




    public String getSequence(String seqName)
    {
        String sql  = "select "+seqName+".nextval from dual ";
        SQLQuery query = getSession().createSQLQuery(sql);
        List list = query.list();
        list.get(0).toString();
        return list.get(0).toString();
    }

    //登录的用户名获取用户
    public  AB01 queryStudent(String name,String userid)
    {
        GenericQuery query = createQuery("obj");
        if(StringUtils.isNotBlank(name))
            query.append(" and obj.AAB001=:name").setParam("name",name);
        if(StringUtils.isNotBlank(userid))
            query.append(" and obj.AAB998=:userid").setParam("userid",userid);
        return  query.singleResult();
    }


    //
    public StudentDTO getBattingInfo(StudentDTO dto)
    {
        StringBuffer sql  = new StringBuffer();
        /*sql.append(" select a.aab001,a.aab002,a.aab007,c.bab001,c.bab002,a.aab999,b.aab996 ");
        sql.append(" from ab01 a,ab02 b,ba01 c");
        sql.append(" where 1=1 ");
        sql.append(" and a.aab001=b.aab001");
        sql.append(" and b.bab001=c.bab001");
        if (StringUtils.isNotBlank(dto.getAAB998()))
            sql.append(" and a.aab998='"+dto.getAAB998()+"'");
        if (StringUtils.isNotBlank(dto.getAAB001()))
            sql.append("  and b.aab001='"+dto.getAAB001()+"'" );
        sql.append(" and b.bab001='"+dto.getBAB001()+"'");*/

        sql.append("  select a.aab001, a.aab002, a.aab007, a.aab999, b.aab996,b.bab001,a.aab998  ");
        sql.append("  from ab01 a , ab02 b ");
        sql.append("  where 1=1 ");
        sql.append("  and a.aab001 =b.aab001(+) ");
        sql.append("  and a.aab998='"+dto.getAAB998()+"'");
        sql.append("  and b.bab001(+) ='"+dto.getBAB001()+"'");
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(StudentDTO.class));
        List<StudentDTO> list =  (List<StudentDTO>) query.list();
        StudentDTO dtos=null;
        if(ObjectUtils.isNotBlank(list))
            dtos =list.get(0);
        else {
            dtos = new StudentDTO();
        }
        return dtos;
    }

    /**
     * 账户余额减少1一个点
     */
    public void updateAccount()
    {
        GenericQuery update= create(" update AB01 t set t.AAB999=AAB999-1 ");
        update.executeUpdate();
    }

    public void updateRestart()
    {
        GenericQuery update= create(" update AB01 t set t.AAB999='10000' ");
        update.executeUpdate();
    }


    public AB01 getAAB999(String aab998)
    {
        GenericQuery query  = createQuery("t");
        query.append(" and t.AAB998=:aab998 ").setParam("aab998",aab998);
        return  query.singleResult();
    }


    /**
     *
     * @param
     * @param aab997  新增点数
     */
    public void changeStudentAcount(String aab997) throws SQLException {

        // ResultSet resultSet = null;

            Connection con = SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
            String call = "{ call changeAccount(?)}";
            CallableStatement cs = con.prepareCall(call);
            cs.setString(1,aab997);

            cs.execute();





    }

}
